package com.lbn.monitor.base;

import com.free.framework.base.BaseFragment;
import com.free.framework.lib.utils.Dp2Px;
import com.free.framework.mvp.BaseMvpPresenter;

import in.srain.cube.views.ptr.PtrDefaultHandler;
import in.srain.cube.views.ptr.PtrFrameLayout;
import in.srain.cube.views.ptr.header.MaterialHeader;

/**
 * @author 李炳南
 * @date 2017/3/6
 */
public abstract class AppBaseFragment<P extends BaseMvpPresenter> extends BaseFragment<P> {


    protected void initPtr(final PtrFrameLayout ptrFrameLayout, final AppBaseActivity.OnPtrRefresh onPtrRefresh){
        initPtr(ptrFrameLayout,onPtrRefresh,true);
    }

    protected void initPtr(final PtrFrameLayout ptrFrameLayout, final AppBaseActivity.OnPtrRefresh onPtrRefresh, boolean auto){

        MaterialHeader header = new MaterialHeader(ptrFrameLayout.getContext());

        int vPadding = Dp2Px.dip2px(ptrFrameLayout.getContext(), 10f);

        header.setPadding(0, vPadding, 0, vPadding);
        ptrFrameLayout.setDurationToCloseHeader(1500);
        ptrFrameLayout.setHeaderView(header);
        ptrFrameLayout.addPtrUIHandler(header);

        ptrFrameLayout.setLoadingMinTime(1000);
        ptrFrameLayout.setPtrHandler(new PtrDefaultHandler() {
            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                onPtrRefresh.onPtrRefresh();
            }
        });

        if(auto){
            ptrFrameLayout.post(new Runnable() {
                @Override
                public void run() {
                    ptrFrameLayout.autoRefresh();
                }
            });
        }

    }


    public interface OnPtrRefresh{

        void onPtrRefresh();

    }

}
